﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagChina.Basic.CCT.Db.Printer_Db;
using Serilog;
using Serilog.Core;
using Serilog.Debugging;
using System;
using System.Linq;

namespace Proxy
{
    public class ModifyInfoHandle
    {
        private CommonHandle Handle_Common;

        public ModifyInfoHandle()
        {
            Handle_Common = new CommonHandle();
        }

        public bool ModifySettingsInfo(T_SettingsConfig t)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    var SettingConfigInfo = Db.T_SettingsConfig.FirstOrDefault(x => x.Id == t.Id);
                    Handle_Common.Mapper<T_SettingsConfig, T_SettingsConfig>(SettingConfigInfo, t);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Modify.Error(ex.Message);
                return result;
            }
        }

        /// <summary>
        /// 更新传输状态
        /// </summary>
        /// <param name="Reference"></param>
        /// <param name="State"></param>
        public void ModifyTransState(T_TransOnlineRecords T)
        {
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    T_TransOnlineRecords t_TransOnlineRecords = Db.T_TransOnlineRecords.FirstOrDefault(s => s.Reference == T.Reference);
                    if (t_TransOnlineRecords != null)
                    {
                        t_TransOnlineRecords.State = T.State;
                        bool f = Db.SaveChanges() > 0;
                    }
                }
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Modify.Error(ex.Message);
            }
        }

        /// <summary>
        /// 更新小板贴标任务状态
        /// </summary>
        /// <param name="Reference"></param>
        /// <param name="State"></param>
        public void ModifyLabelPrintState_Part(int SourceState, int TargetState, Logger log)
        {
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    T_Nesting_LabelTask t_Nesting_LabelTask = Db.T_Nesting_LabelTask.FirstOrDefault(s => s.State == SourceState);
                    if (t_Nesting_LabelTask != null)
                    {
                        t_Nesting_LabelTask.State = TargetState;
                        bool f = Db.SaveChanges() > 0;
                    }
                    log.Information(t_Nesting_LabelTask.Reference + "Part生产状态由：" + SourceState + "变更为：" + TargetState);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }

        /// <summary>
        /// 更新小板贴标任务状态
        /// </summary>
        /// <param name="Reference"></param>
        /// <param name="State"></param>
        public void ModifyLabelPrintStateForRePrint(int TaskId, int TargetState, Logger log)
        {
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    T_Nesting_LabelTask t_Nesting_LabelTask = Db.T_Nesting_LabelTask.FirstOrDefault(s => s.Id == TaskId);
                    if (t_Nesting_LabelTask != null)
                    {
                        t_Nesting_LabelTask.State = TargetState;
                        bool f = Db.SaveChanges() > 0;
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }

        /// <summary>
        /// 更新大板贴标任务状态
        /// </summary>
        /// <param name="Reference"></param>
        /// <param name="State"></param>
        public void ModifyLabelPrintState_Board(int SourceState, int TargetState, Logger log)
        {
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    T_Nesting_BoardInfo t_Nesting_BoardState = Db.T_Nesting_BoardInfo.FirstOrDefault(s => s.State == SourceState);
                    if (t_Nesting_BoardState != null)
                    {
                        t_Nesting_BoardState.State = TargetState;
                        bool f = Db.SaveChanges() > 0;
                    }
                    log.Information(t_Nesting_BoardState.MprName + "Board生产状态由：" + SourceState + "变更为：" + TargetState);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }

        /// <summary>
        /// 更新大板贴标任务状态
        /// </summary>
        /// <param name="Reference"></param>
        /// <param name="State"></param>
        public void ModifyState_Board(string BoardId, int TargetState)
        {
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    T_Nesting_BoardInfo t_Nesting_BoardState = Db.T_Nesting_BoardInfo.FirstOrDefault(s => s.BoardId == BoardId);
                    if (t_Nesting_BoardState != null)
                    {
                        t_Nesting_BoardState.State = TargetState;
                        bool f = Db.SaveChanges() > 0;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// 更新批次贴标任务状态
        /// </summary>
        /// <param name="Reference"></param>
        /// <param name="State"></param>
        public void ModifyLabelPrintState_Batch(string BatchId, int TargetState, Logger log)
        {
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    T_Nesting_BatchInfo t_Nesting_BatchInfo = Db.T_Nesting_BatchInfo.FirstOrDefault(s => s.BatchId == BatchId);
                    if (t_Nesting_BatchInfo != null)
                    {
                        t_Nesting_BatchInfo.State = TargetState;
                        bool f = Db.SaveChanges() > 0;
                    }
                    log.Information(t_Nesting_BatchInfo.BatchId + "Batch生产变更为：" + TargetState);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }

        /// <summary>
        /// 更新路径
        /// </summary>
        /// <param name="Reference"></param>
        /// <param name="State"></param>
        public void ModifyTransRouteId(string Reference, int RouteId)
        {
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    T_TransOnlineRecords t_TransOnlineRecords = Db.T_TransOnlineRecords.FirstOrDefault(s => s.Reference.Equals(Reference));

                    t_TransOnlineRecords.RouteId = RouteId;

                    Db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}